/**
 * Created by jiangna on 2016/12/07 14:00;
 */
var vm = new Vue({
    el: '#main',
    data: {
        branchName: '', //机构名称
        branchAbbr: '', // 机构简称
        branchLevel: '', // 机构等级
        status: '', // 机构状态
        statusList: [{status:'失效', value:0},{status:'有效', value:1}], // 机构状态
        queryList:[], // 查询列表
        tipMsg: '', // 提示信息
        nowPage:1,
        rowNum:10,
        allPages:0,
        pagination: [],
        queryData:{
            nowPage:1,
            rowNum:10,
            object:{}
        },
        title: '新增机构',  // 标题
        edit: false,  // 编辑时，显示状态一项
        flag: '', // 新增--add ， 编辑--edit
        branchId: '', // 机构id
        role:{},
        roleTypes:[],
        agentCates:[],
        jumpPage: '',
        listLength:'',//列表总长度
    },
    created: function () {
        this.query()

        setTimeout(function(){
            $('#roleType').on('change', function() {
                vm.queryData.object.roleType = $(this).val();
            });
            $('#agentCate').on('change', function() {
                vm.queryData.object.agentCate = $(this).val();
            });
        }, 1);
    },
    methods: {
        //获取选中的状态码
        statusValue(e){
            console.log(e)
            this.status = e.target.value
            console.log(this.status)
        },
        //分页
        selectPage(page){
            this.nowPage = page;
            this.query();
        },
        skipPage() {
        	this.nowPage = document.getElementById('page-num').value;
        	document.getElementById('page-num').value = '';
        	this.query();
        },
        offsetPage(offset){
            this.nowPage = this.nowPage + offset;
            this.query();
        },
        // 查询功能
        query() {
            var data = {
                branchName:  this.branchName,
                branchAbbr:  this.branchAbbr,
                branchLevel: this.branchLevel,
                status:	this.status,
                nowPage: this.nowPage,
                rowNum: this.rowNum
            }
            axios({
                method: 'post',
                url: App.envUrl.backtrackUrl + 'branch/query_branch_page',
                // url: 'http://192.168.1.203:8080/slxt-web/branch/query_branch_page',  // 炜袁本地
                data: data
            })
                .then((data) => {
                    if(data.data.datas.length == 0) {
                        this.tipMsg = '未查询到符合条件的数据！'
                        $('#my-alert1').modal({
                            relatedTarget: this,
                        });
                    }
                    if(data.data.datas.length >0){
                        this.queryList = data.data.datas
                        this.allPages = data.data.allPages;
                        this.pagination = getPagination(this.nowPage,this.allPages);
                        this.listLength = data.data.allCount;
                    }else{
                    	this.queryList = [];
                    	this.allPages = data.data.allPages;
                        this.pagination = getPagination(this.nowPage,this.allPages);
                        this.listLength = data.data.allCount;
                    }
                    console.log('机构管理=',data)

                })
                .catch((err) => {});
        },

        //重置按钮
        resetBtn:function(){
            window.location.reload();
        },
        // 新增按钮
        addBtn:function(){
            $('#my-alert').modal({
                relatedTarget: this,
            });
            this.branchId = '';
            this.branchName = '';
            this.branchAbbr = '';
            $('#add-name').text('');
            $('#add-nameJC').text('');
        },
        // 编辑按钮
        gotoPage:function(item){
            $('#my-alert2').modal({
                relatedTarget: this,
            });
            this.branchId = item.branchId;
            this.branchName = item.branchName;
            this.branchAbbr = item.branchAbbr;
            this.status = item.status;
            $('#edit-name').text('');
            $('#edit-nameJC').text('');
            $('#edit-level').text('');
            this.editquery();
        },
        //返回
        goBtn:function(){
            window.location.href = "organization.html";
        },
        // 删除按钮
        del(id) {
            console.log(id)
            var data = {
                seqId: id,
            }
            axios({
                method: 'post',
                url: App.envUrl.backtrackUrl + 'branch/delete_branch',
                // url: 'http://192.168.1.203:8080/slxt-web/branch/delete_branch',  // 炜袁本地
                data: data,
            })
                .then((data)=> {
                    console.log(data.data.ret)
                    if(data.data.ret == '00000') {
                        this.query()
                    }
                    console.log(data)
                })
                .catch((err)=> {

                })
        },
        // 保存接口
        add() {
            if(!this.branchName){
                $('#add-name').text('机构名称不能为空');
                return;
            }else if (!this.branchAbbr){
                $('#add-name').text('');
                $('#add-nameJC').text('机构简称不能为空');
                return;
            }else{
                var data = {
                    branchName: this.branchName,
                    branchAbbr: this.branchAbbr,
                    branchId:this.branchId,
                }
                axios({
                    method: "post",
                    url: App.envUrl.backtrackUrl+"branch/add_branch",
                    // url: 'http://192.168.1.203:8080/slxt-web/branch/add_branch',  // 炜袁本地
                    data: data
                }).then((data) => {
                    if(data.data.ret == '00000') {
                        window.location.href = "organization.html";
                    }
                    console.log('新增=',data)
                }).catch((err) => {})
            }
        },
        //编辑页面保存
        editfn(){
            if(!this.branchName){
                $('#edit-name').text('机构名称不能为空');
                return;
            }else if (!this.branchAbbr){
                $('#edit-name').text('');
                $('#edit-nameJC').text('机构简称不能为空');
                return;
            }else if(!this.status){
                $('#edit-name').text('');
                $('#edit-nameJC').text('');
                $('#edit-level').text('机构状态不能为空');
                return;
            }else{
                var data = {
                    branchId: this.branchId,
                    branchName: this.branchName,
                    branchAbbr: this.branchAbbr,
                    status: this.status,
                    seqId:this.seqId,
                }
                axios({
                    method: "post",
                    url: App.envUrl.backtrackUrl+"branch/update_branch",
                    // url: 'http://192.168.1.203:8080/slxt-web/branch/update_branch',  // 炜袁本地
                    data: data
                }).then((data) => {
                    if(data.data.ret == '00000') {
                        window.location.href = "organization.html";
                    }
                    console.log('编辑=',data)
                }).catch((err) => {})
            }
        },
        // 编辑页面初始化数据
        editquery() {
            data = {
                branchId: this.branchId
            }
            console.log(data)

            axios({
                method: 'post',
                url: App.envUrl.backtrackUrl + 'branch/query_branch',
                // url: 'http://192.168.1.203:8080/slxt-web/branch/query_branch',  // 炜袁本地
                data: data
            })
                .then((data) => {
                    if(data.data.ret == '00000') {
                        this.branchName = data.data.data.branchName
                        this.branchAbbr = data.data.data.branchAbbr
                        this.status = data.data.data.status;
                        this.seqId = data.data.data.seqId;
                    }
                })
                .catch((err) => {});
        },

    }
});

function rowNumChange(value){
    vm.queryData.rowNum = value;
    vm.queryData.nowPage = 1;
    vm.queryRoles();
}

